﻿package racingGame.engine 
{
	import away3d.cameras.Camera3D;
	import away3d.cameras.HoverCamera3D;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.core.math.Number3D;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.ITriangleMaterial;
	import away3d.materials.WireColorMaterial;
	import away3d.materials.WireframeMaterial;
	import away3d.primitives.Plane;
	import away3d.primitives.Skybox6;
	import away3d.primitives.Sphere;
	import flash.events.MouseEvent;
	import racingGame.assets.Textures;
	
	import collada.DAE;
	import flash.events.Event;
	import racingGame.assets.Meshes;
	import racingGame.models.Track;
	
	/**
	 * ...
	 * @author Jorge Miranda
	 */
	public class TrackTestEngine 
	{
		/**** Private Variables ****/
		
		private var _view		:View3D;
		private var _camera		:HoverCamera3D;
		private var _scene		:Scene3D;
		private var _track		:Track;
		private var _dae		:DAE;
		private var _skybox:Skybox6;
		private var _sky_material:ITriangleMaterial;
		
		private var move:Boolean = false;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		private var lastMouseX:Number;
		private var lastMouseY:Number;
		
		
		/**** Public Methods ****/		
		
		/**
		 * Constructor.
		 */
		public function TrackTestEngine(view:View3D) 
		{
			_view = view;
			_scene = _view.scene;
			
			initializeWorld();
		}
		
		private function initializeWorld():void
		{			
			
			initializeSky();
			
			_dae = new DAE();
			_dae.parse(Meshes.TEST_LEVEL);
			initializeTrack();			
			
			initializeCamera();
			
			initListeners();
		}
		
		private function initializeCamera():void
		{
			_camera = new HoverCamera3D();
			_camera.focus = 30;
			//_camera.mintiltangle = -80;
			//_camera.maxtiltangle = 20;
			
			//_camera.fov = 6
			
			_camera.distance = 100;
			
			_camera.targetpanangle = _camera.panangle = 0;
			_camera.targettiltangle = _camera.tiltangle = 0;
			
			_view.camera = _camera;		
			/*
			_camera.x = _track.segments[0].start.x;
			_camera.z = _track.segments[0].start.z;
			_camera.y = _track.segments[0].start.y + 30;*/
		}
		
		private function initializeSky():void
		{
			_sky_material = new BitmapMaterial(Textures.SKY);
			_skybox = new Skybox6(_sky_material);
			_skybox.quarterFaces();
			_skybox.scale(.5);
			
			_scene.addChild(_skybox);
		}
		
		private function initializeTrack():void
		{
			
			_track = Track.createFromDAE(_dae);
			
			_scene.addChild(_track.mesh);
			var plane:Plane = new Plane( { material:new WireframeMaterial(0x0), width:200, height:200, segmentsH:10, segmentsW:10 } );
			
			plane.y = -5;
			//_scene.addChild(plane);
		}
		
		
		/**
		 * Initialise the listeners
		 */
		private function initListeners():void
		{
			_view.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_view.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		
		public function update():void
		{
			if (move) {
				_camera.targetpanangle = 0.3*(_view.stage.mouseX - lastMouseX) + lastPanAngle;
				_camera.targettiltangle = 0.3*(_view.stage.mouseY - lastMouseY) + lastTiltAngle;
			}
			
			_camera.hover();
		}
		
		/**
		 * Mouse down listener for navigation
		 */
		private function onMouseDown(event:MouseEvent):void
        {
            lastPanAngle = _camera.targetpanangle;
            lastTiltAngle = _camera.targettiltangle;
            lastMouseX = _view.stage.mouseX;
            lastMouseY = _view.stage.mouseY;
        	move = true;
        	_view.stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
        }
		
		/**
		 * Mouse up listener for navigation
		 */
        private function onMouseUp(event:MouseEvent):void
        {
        	move = false;
        	_view.stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     
        }
        
		/**
		 * Mouse stage leave listener for navigation
		 */
        private function onStageMouseLeave(event:Event):void
        {
        	move = false;
        	_view.stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     
        }
		
	}
	
}